﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.IO;

namespace ACLibs.Config
{
    /// <summary>
    /// 使用数据库存储配置
    /// </summary>
    public class SQLiteConfig
    {
        private static string ConfigFileName = "AppConfig";
        private static string DefaultTableName = "Default";

        /// <summary>
        /// 初始化配置文件使用SQLite的数据库文件
        /// </summary>
        /// <param name="ConfigFileName">配置文件名称</param>
        /// <param name="NewConfigFile">是否覆盖老文件</param>
        /// <returns></returns>
        public static void Init(string ConfigFileName,bool NewConfigFile=true)
        {
            string ConfigFilePath = Directory.GetCurrentDirectory() + ".\\" + ConfigFileName;

            try
            {
                if (File.Exists(ConfigFilePath))
                {
                    if (NewConfigFile)
                    {
                        File.Delete(ConfigFilePath);
                    }
                }
                else
                {
                    SQLiteConnection.CreateFile(ConfigFilePath);
                }
                using (SQLiteConnection conn = new SQLiteConnection($"Data Source={ConfigFilePath};Version=3;"))
                {
                    using(SQLiteCommand comm = conn.CreateCommand())
                    {
                        comm.CommandText = $"Create Table {DefaultTableName} (Key TEXT PRIMARY KEY,Value TEXT,Remark TEXT);";
                        comm.ExecuteNonQuery();
                    }
                }
            }
            catch
            {
                throw;
            }
            
        }
    }
}
